package com.jaj.services;

import java.util.List;

import com.jaj.common.JavaDocReader;
import com.jaj.models.RapGroup;
import com.jaj.models.RapProductLine;
import com.jaj.models.RapProject;
import com.jaj.services.rap.GroupService;
import com.jaj.services.rap.LoginService;
import com.jaj.services.rap.ProjectLineService;
import com.jaj.services.rap.ProjectService;
import com.jaj.services.rap.RapConfig;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.RootDoc;

/**
 * Created by chm on 2018/1/25.
 *
 * @author jaj
 */
public class RapGenerateService {

    public static void main(String[] args) {
        //1意见反馈与评论
//        String path="D:\\sonlidev\\FeedBackSystem\\master\\witparking-feedback.interfaces\\src\\main\\java\\org\\witparking\\feedback\\interfaces";
//        String projectName="意见反馈与评论";
        //2长租项目
//        String path="D:\\sonlidev\\WitparkingLeaseNew\\lease\\witparking-lease.interfaces\\src\\main\\java\\org\\witparking\\lease\\interfaces";
//        String projectName="长租项目";
        //3共享泊位
//        String path="D:\\sonlidev\\ShareCarport\\ShareOrder\\witparking-share.interfaces\\src\\main\\java\\org\\witparking\\sharecarport\\interfaces";
//        String projectName="共享泊位";
        //4共享泊位-查询项目
//        String path="D:\\sonlidev\\ShareCarport\\ShareQuery\\witparking-sharequery.interfaces\\src\\main\\java\\org\\witparking\\sharecarport\\interfaces";
//        String projectName="共享泊位-查询项目";
        //5 CMS项目
        String path="D:\\sonlidev\\WisdomPayment\\sonlicms\\Sonli.Cms-Interfaces\\src\\main\\java\\sonli\\cms\\interfaces";
        String projectName="CMS项目";

        generateRap("zhangsan","password",path,"zhangsan(张三),lisi(李四)",projectName);
    }

    public static void generateRap(String userName,String password,String path,String accounts,String projectName) {
        String sessionId = LoginService.rapLogin(userName, password);
        //0先删除以前生成的
        deleteProductLineByName(sessionId,projectName);
        //1创建产品线，并返回Id
        String plid = ProjectLineService.createProductLine(sessionId, RapConfig.Team.ShareTeamId, projectName);
        //2创建分组，并返回Id
        String groupId = GroupService.createGroup(sessionId, plid, projectName + "接口列表");
        RootDoc root= JavaDocReader.read(path);
        for (ClassDoc classDoc : root.classes()) {
            //3创建项目，并返回Id
            String projectId = ProjectService.createProject(sessionId, groupId, classDoc.name(), accounts, classDoc.commentText());
            //4编辑项目中具体的接口
            ProjectService.checkIn(sessionId, classDoc, projectId);
        }
    }

    /**
     * 删除产品线
     * @param sessionId 用户的Sessionid
     * @param productLineName 产品线名字
     */
    private static void deleteProductLineByName(String sessionId,String productLineName){
        //1获取所有的产品线
        List<RapProductLine> rapProductLineList= ProjectLineService.getAllProductLine(sessionId, RapConfig.Team.ShareTeamId);
        if(rapProductLineList!=null&&rapProductLineList.size()>0){
            //2根据产品线名字获取对应的Id
            RapProductLine rapProductLine=null;
            for (RapProductLine temp : rapProductLineList) {
               if(temp.getName().equals(productLineName)){
                   rapProductLine=temp;
                   break;
               }
            }
            //3根据产品线id获取所有分组
            if(rapProductLine!=null){
                List<RapGroup> rapGroupList= GroupService.getAllGroups(sessionId,rapProductLine.getId());
                if(rapGroupList!=null&&rapGroupList.size()>0){
                    for (RapGroup rapGroup : rapGroupList) {
                        if(rapGroup.getProjects()!=null&&rapGroup.getProjects().size()>0){
                            //4先删除所有的projects
                            for (RapProject rapProject : rapGroup.getProjects()) {
                                ProjectService.deleteProject(sessionId,rapProject.getId());
                            }
                        }
                        //5然后删除所有的分组
                        GroupService.deleteGroup(sessionId,rapGroup.getId());
                    }
                }
                //最后删除产品线
                ProjectLineService.deleteProductLine(sessionId,rapProductLine.getId());
            }
        }
    }



}
